<?php

	function eventGET($request){
		$getById = false;
		$event = new Event();
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$event->setEventId($request->getArgs['id']);
			$getById = true;
		}
		
		$session = unserialize($_SESSION['smarthotel']['session']);
		$user = $session->getUser();
		$event->getEventUser()->setUser($user);
		
		$eventBusiness = BusinessFactory::factory('EventBusiness');
		$result = $eventBusiness->select($event);
		
		if ($result != NULL) {
		
			if(is_array($result)) {
				if($getById) {
					if (sizeof($result) == 1) {
						header("Status: 200 Success", true, 200);
						return $result[0];
					}
					else {
						header("Status: 404 Not Found", true, 404);
					}
				}
				else {
					header("Status: 200 Success", true, 200);
					return $result;
				}
				
				
			} else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		} 
		
		return $result;
	}
	
	function eventPOST($request){
		//TODO
	}
	
	function eventuserGET($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventId = $request->getArgs['id'];
			
			$eventUserBusiness = BusinessFactory::factory('EventUserBusiness');
			$result = $eventUserBusiness->selectEventUserByEventId($eventId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventuserPOST($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$event = new Event();
			$event->setEventId($request->getArgs['id']);
			
			
			$userJSON = json_decode(utf8_decode($request->postArgs['user']));
			//TODO pensar em como viria o isManager
			$event->getEventUser()->setIsUserManager($request->postArgs['isUserManager']);
			$event->getEventUser()->setIsUserConfirmed($request->postArgs['isUserConfirmed']);
			$user = new User();
			$user->JSONToObject($userJSON);
			$event->getEventUser()->setUser($user);
			
			$session = unserialize($_SESSION['smarthotel']['session']);
			$invitatioUser = $session->getUser();
			
			if ($invitatioUser->getUserId() != $user->getUserId()) {
				$event->getEventUser()->setInvitationUser($invitatioUser);
			}
			$eventUserBusiness = BusinessFactory::factory('EventUserBusiness');
			$result = $eventUserBusiness->saveEventAttendant($event);
			
			if ($result != -1) {
				header("Status: 201 Created", true, 201);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}

	function eventuserPUT($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$event = new Event();
			$event->setEventId($request->getArgs['id']);
			
			
			$userJSON = json_decode(utf8_decode($request->putArgs['user']));
			//TODO pensar em como viria o isManager
			$event->getEventUser()->setIsUserConfirmed($request->putArgs['isUserConfirmed']);
			$user = new User();
			$user->JSONToObject($userJSON);
			$event->getEventUser()->setUser($user);
			$eventUserBusiness = BusinessFactory::factory('EventUserBusiness');
			$result = $eventUserBusiness->updateEventAttendant($event);
			
			if ($result) {
				header("Status: 200 Ok", true, 200);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventuserDELETE($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$event = new Event();
			$event->setEventId($request->getArgs['id']);
						
			$event->getEventUser()->setUserRegistrationCode($request->deleteArgs['userRegistrationCode']);
			
			$eventUserBusiness = BusinessFactory::factory('EventUserBusiness');
			$result = $eventUserBusiness->deleteEventAttendant($event);
			
			if ($result) {
				header("Status: 200 Ok", true, 200);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventnotinviteduserGET($request) {
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventId = $request->getArgs['id'];
			
			$eventUserBusiness = BusinessFactory::factory('EventUserBusiness');
			$result = $eventUserBusiness->selectNotInvitedUserByEventId($eventId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
		
	function eventItemGET($request) {
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventId = $request->getArgs['id'];
			
			$eventItemBusiness = BusinessFactory::factory('EventItemBusiness');
			$result = $eventItemBusiness->selectEventItemByEventId($eventId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventnotificationGET($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventId = $request->getArgs['id'];
			
			$eventNotificationBusiness = BusinessFactory::factory('EventNotificationBusiness');
			$result = $eventNotificationBusiness->selectEventNotificationByEventId($eventId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	
	function eventnotificationPOST($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
						
			$notificationJSON = json_decode(utf8_decode($request->postArgs['notification']));
			$notification = new Notification();
			$notification->JSONToObject($notificationJSON);
			
			$eventNotificationBusiness = BusinessFactory::factory('EventNotificationBusiness');
			$result = $eventNotificationBusiness->saveNotification($notification);
			
			if ($result != -1) {
				header("Status: 201 Created", true, 201);
				header("Location: " . $request->getActualUrl() . $result);
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	

?>